package com.jingdianjichi.project.domain.handler.subject;

import com.jingdianjichi.project.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.project.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.project.domain.bo.SubjectAnswerBo;
import com.jingdianjichi.project.domain.bo.SubjectInfoBo;
import com.jingdianjichi.project.domain.bo.SubjectOptionBo;
import com.jingdianjichi.project.domain.convert.MultipleSubjectConverter;
import com.jingdianjichi.project.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.project.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * ClassName:RadioTypeHandler
 * Package:com.jingdianjichi.subject.domain.handler.subject
 * Description:
 *
 * @Author fst
 * @Create 2024/9/29 13:29
 * @Version 1.0
 */


/**
 * 多选题目
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler
{

    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo)
    {
        //多选题目的插入
        List<SubjectMultiple> subjectMultipleList= new LinkedList<>();

        for (SubjectAnswerBo subjectAnswerBo : subjectInfoBo.getOptionList()) {
            SubjectMultiple subjectMultiple = MultipleSubjectConverter.INSTANCE.convertBoTOEntity(subjectAnswerBo);
            subjectMultiple.setSubjectId(subjectInfoBo.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectMultipleList.add(subjectMultiple);
        }

        subjectMultipleService.BatchInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBo query(int subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));
        List<SubjectMultiple> result = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBo> subjectAnswerBOList = MultipleSubjectConverter.INSTANCE.convertEntityToBoList(result);
        SubjectOptionBo subjectOptionBO = new SubjectOptionBo();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;

    }
}
